package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.LeadDataRs;
import com.xmy.cultivate.entity.views.SchoolRs;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 内容【数据】权限 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-04-26
 */
@RestController
@RequestMapping("/api/authAdminContent")
public class AuthAdminContentController extends BaseController {


    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    IAuthAdminContentService iAuthAdminContentService;

    @Autowired
    IOrganizationService iOrganizationService;

    @Autowired
    IOrganizationDepartmentPrincipalService iOrganizationDepartmentPrincipalService;

    @Autowired
    IAuthTeacherClassifyService iAuthTeacherClassifyService;

    @Autowired
    ISchoolQuarterService iSchoolQuarterService;

    @Autowired
    IGradeService iGradeService;

    @GetMapping("/getAuthData")
    public Result getAuthData(@RequestParam(value = "yearPart", required = false) Integer yearPart,
                              @RequestParam(value = "quarterNum", required = false) Integer quarterNum) {
        Quarter quarterNow = iQuarterService.getNowQuarter();

        if (yearPart == null) {
            yearPart = quarterNow.getYearPart();
        }
        if (quarterNum == null) {
            quarterNum = quarterNow.getNum();
        }

        AuthAdminContent authAdminContent = iAuthAdminContentService.getPermissionForTeacherId(yearPart, quarterNum, this.adminId, this.baseSchoolIdStr);
        LeadDataRs leadDataRs = authAdminContent.getLeadDataRs();
        String yearClassIds = CommonUtil.getStrUnion(leadDataRs.getYearClassIds().replace("-1", ""), authAdminContent.getYearClassIds().replace("-1", ""));
        authAdminContent.setYearClassIds(yearClassIds);
        String subjectsIds = CommonUtil.getStrUnion(leadDataRs.getSubjectsIds().replace("-1", ""), authAdminContent.getSubjectsIds().replace("-1", ""));
        authAdminContent.setSubjectsIds(subjectsIds);
        return new Result(Code.OK, authAdminContent, "获取成功");
    }

    @GetMapping("/getAuthSchool")
    public Result getAuthSchool(@RequestParam(value = "yearPart", required = false) Integer yearPart,
                                @RequestParam(value = "quarterNum", required = false) Integer quarterNum
    ) {
        if (yearPart == null) {
            Quarter quarterNow = iQuarterService.getNowQuarter();
            yearPart = quarterNow.getYearPart();
            quarterNum = quarterNow.getNum();
        }

        AuthAdminContent authAdminContent = iAuthAdminContentService.getPermissionForTeacherId(yearPart, quarterNum, this.adminId, this.baseSchoolIdStr);
        LeadDataRs leadDataRs = authAdminContent.getLeadDataRs();
        String schoolIds = CommonUtil.getStrUnion(leadDataRs.getSchoolIds().replace("-1", ""), authAdminContent.getSchoolIds().replace("-1", ""));
        List<SchoolRs> schoolRsList = new ArrayList<>();
        if (StringUtils.isNotBlank(schoolIds)) {
            QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>();
            organizationQueryWrapper.inSql("id", schoolIds);
            List<Organization> organizationList = iOrganizationService.list(organizationQueryWrapper);
            for (Organization organization : organizationList) {
                SchoolRs schoolRs = new SchoolRs();
                schoolRs.setId(organization.getId());
                schoolRs.setName(organization.getName());
                schoolRsList.add(schoolRs);
            }
        }

        return new Result(Code.OK, new PageResult(schoolRsList, (long) schoolRsList.size(), 1L));
    }

    @GetMapping("/getAuthDataV2")
    public Result getAuthDataV2(@RequestParam(value = "yearPart", required = false) Integer yearPart,
                                @RequestParam(value = "quarterNum", required = false) Integer quarterNum
    ) {
        Quarter quarterNow = iQuarterService.getNowQuarter();

        if (yearPart == null) {
            yearPart = quarterNow.getYearPart();
        }
        if (quarterNum == null) {
            quarterNum = quarterNow.getNum();
        }

        AuthAdminContent authAdminContent = iAuthAdminContentService.getPermissionForTeacherId(yearPart, quarterNum, this.adminId, this.baseSchoolIdStr);
        LeadDataRs leadDataRs = authAdminContent.getLeadDataRs();
//        String yearClassIds = CommonUtil.getStrUnion(leadDataRs.getYearClassIds().replace("-1", ""), authAdminContent.getYearClassIds().replace("-1", ""));
//        String subjectsIds = CommonUtil.getStrUnion(leadDataRs.getSubjectsIds().replace("-1", ""), authAdminContent.getSubjectsIds().replace("-1", ""));

        Integer isManager = iOrganizationService.getIsManager(yearPart, quarterNum, this.adminId);

        // 判断是否为主管
        OrganizationDepartmentPrincipal organizationDepartmentPrincipal1 = iOrganizationDepartmentPrincipalService.getByDepartmentId("23,27,24,25", 1L, this.adminId, yearPart, quarterNum);

        // 判断是否为年级负责人
        OrganizationDepartmentPrincipal organizationDepartmentPrincipal2 = iOrganizationDepartmentPrincipalService.getByDepartmentId("23", 2L, this.adminId, yearPart, quarterNum);

        // 判断是否为火种老师
        AuthTeacherClassify authTeacherClassify1 = iAuthTeacherClassifyService.getTeacherClassifyByTeacherId(this.adminId, 3);

        // 判断是否为个人老师
        AuthTeacherClassify authTeacherClassify2 = iAuthTeacherClassifyService.getTeacherClassifyByTeacherId(this.adminId, 2);

        if (isManager > 0) {
            // 校长
            authAdminContent.setYearClassIds("0,1,2,3,4,5,6,7,8,9,10,11,12");
            authAdminContent.setSubjectsIds("1,2,3");
        } else if (organizationDepartmentPrincipal1 != null) {
            // 主管
            authAdminContent.setYearClassIds("0,1,2,3,4,5,6,7,8,9,10,11,12");
            authAdminContent.setSubjectsIds("1,2,3");
        } else if (organizationDepartmentPrincipal2 != null) {
            // 年级负责人
            // 带班年级
            String yearClassIds = iGradeService.getYearClassIdForTeacherId(yearPart, quarterNum, this.adminId);
            // 负责年级
            String yearClassIds2 = iOrganizationDepartmentPrincipalService.getYearClassIdsForTeacherId(yearPart, quarterNum, this.adminId, 2);
            if (StringUtils.isNotBlank(yearClassIds) && StringUtils.isNotBlank(yearClassIds2)) {
                yearClassIds += "," + yearClassIds2;
            } else if (StringUtils.isNotBlank(yearClassIds2)) {
                yearClassIds = yearClassIds2;
            }
            authAdminContent.setSchoolIds(iSchoolQuarterService.getSchoolIdStr(yearPart, quarterNum));
            authAdminContent.setYearClassIds(yearClassIds);
            authAdminContent.setSubjectsIds("1,2,3");

        } else if (authTeacherClassify1 != null) {
            // 火种
            String yearClassIds = iGradeService.getYearClassIdForTeacherId(yearPart, quarterNum, this.adminId);
            authAdminContent.setSchoolIds(iSchoolQuarterService.getSchoolIdStr(yearPart, quarterNum));
            authAdminContent.setYearClassIds(yearClassIds);
            authAdminContent.setSubjectsIds("1,2,3");
        } else if (authTeacherClassify2 != null) {
            // 个人
            authAdminContent.setYearClassIds(leadDataRs.getYearClassIds());
            authAdminContent.setSubjectsIds(leadDataRs.getSubjectsIds());
        }

        return new Result(Code.OK, authAdminContent, "获取成功");
    }

    @GetMapping("/getAuthSchoolV2")
    public Result getAuthSchoolV2(@RequestParam(value = "yearPart", required = false) Integer yearPart,
                                  @RequestParam(value = "quarterNum", required = false) Integer quarterNum) {
        if (yearPart == null) {
            Quarter quarterNow = iQuarterService.getNowQuarter();
            yearPart = quarterNow.getYearPart();
            quarterNum = quarterNow.getNum();
        }

        Integer isManager = iOrganizationService.getIsManager(yearPart, quarterNum, this.adminId);

        // 判断是否为主管
        OrganizationDepartmentPrincipal organizationDepartmentPrincipal1 = iOrganizationDepartmentPrincipalService.getByDepartmentId("23,27,24,25", 1L, this.adminId, yearPart, quarterNum);

        // 判断是否为年级负责人
        OrganizationDepartmentPrincipal organizationDepartmentPrincipal2 = iOrganizationDepartmentPrincipalService.getByDepartmentId("23", 2L, this.adminId, yearPart, quarterNum);

        // 判断是否为火种老师
        AuthTeacherClassify authTeacherClassify1 = iAuthTeacherClassifyService.getTeacherClassifyByTeacherId(this.adminId, 3);

        // 判断是否为个人老师
        AuthTeacherClassify authTeacherClassify2 = iAuthTeacherClassifyService.getTeacherClassifyByTeacherId(this.adminId, 2);

        List<SchoolRs> schoolRsList = new ArrayList<>();
        AuthAdminContent authAdminContent = iAuthAdminContentService.getPermissionForTeacherId(yearPart, quarterNum, this.adminId, this.baseSchoolIdStr);
        LeadDataRs leadDataRs = authAdminContent.getLeadDataRs();
        String schoolIds = CommonUtil.getStrUnion(leadDataRs.getSchoolIds().replace("-1", ""), authAdminContent.getSchoolIds().replace("-1", ""));

        if (isManager > 0) {
            // 校长
            schoolIds = iSchoolQuarterService.getSchoolIdStr(yearPart, quarterNum);
        } else if (organizationDepartmentPrincipal1 != null) {
            // 主管
            schoolIds = iSchoolQuarterService.getSchoolIdStr(yearPart, quarterNum);
        } else if (organizationDepartmentPrincipal2 != null) {
            // 年级负责人
            schoolIds = iSchoolQuarterService.getSchoolIdStr(yearPart, quarterNum);
        } else if (authTeacherClassify1 != null) {
            // 火种
            schoolIds = iSchoolQuarterService.getSchoolIdStr(yearPart, quarterNum);
        } else if (authTeacherClassify2 != null) {

        }

        if (StringUtils.isNotBlank(schoolIds)) {
            QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>();
            organizationQueryWrapper.inSql("id", schoolIds);
            List<Organization> organizationList = iOrganizationService.list(organizationQueryWrapper);
            for (Organization organization : organizationList) {
                SchoolRs schoolRs = new SchoolRs();
                schoolRs.setId(organization.getId());
                schoolRs.setName(organization.getName());
                schoolRsList.add(schoolRs);
            }
        }

        return new Result(Code.OK, new PageResult(schoolRsList, (long) schoolRsList.size(), 1L));
    }

}
